﻿using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
using System.Windows.Controls;

namespace LightSwitchApplication
{
    public partial class DrugStock
    {
        partial void Drug_Loaded(bool succeeded)
        {
            // Write your code here.
            this.SetDisplayNameFromEntity(this.Drug);
        }

        partial void Drug_Changed()
        {
            // Write your code here.
            this.SetDisplayNameFromEntity(this.Drug);
        }

        partial void DrugStock_Saved()
        {
            // Write your code here.
            this.SetDisplayNameFromEntity(this.Drug);
        }

        partial void Ok_Execute()
        {
            // Write your code here.
            this.CloseModalWindow("NewPurchaseOrder");
            updateGoodsInShipping(); //update
        }

        partial void Cancel_Execute()
        {
            // Write your code here.
            this.CloseModalWindow("NewPurchaseOrder");
            this.PurchaseOrdersOfDrug.SelectedItem.Details.DiscardChanges();
        }

        partial void DrugStock_Created()
        {
            // Write your code here.
            this.FindControl("NewPurchaseOrder").ControlAvailable += (sender, args) =>
            {
                ChildWindow dialog = args.Control as ChildWindow;
                dialog.Closed += (s, a) => { if (!dialog.DialogResult.HasValue || dialog.DialogResult == false) PurchaseOrdersOfDrug.SelectedItem.Details.DiscardChanges(); };
            };
        }

        partial void PlacePurchaseOrder_CanExecute(ref bool result)
        {
            // Write your code here.
            result = this.Supplies != null &&
                        this.Supplies.SelectedItem != null &&
                        this.Supplies.SelectedItem.IsDiscontinued == false;
        }

        partial void PlacePurchaseOrder_Execute()
        {
            // Write your code here.
            PurchaseOrdersOfDrug.AddNew();
            Supply cheapest = this.Supplies.FirstOrDefault();
            if (cheapest != null)
            {
                PurchaseOrdersOfDrug.SelectedItem.Supply = cheapest;
                int shortage = Drug.MinimumStock - Drug.Stock - GoodsInShipping;
                PurchaseOrdersOfDrug.SelectedItem.Quantity = shortage > 1 ? shortage : 1;
                this.OpenModalWindow("NewPurchaseOrder");
            }
            else
                PurchaseOrdersOfDrug.SelectedItem.Details.DiscardChanges();            
        }

        partial void CancelPurchaseOrder_CanExecute(ref bool result)
        {
            // Write your code here.
            result = this.PurchaseOrdersOfDrug != null &&
                this.PurchaseOrdersOfDrug.SelectedItem != null &&
                this.PurchaseOrdersOfDrug.SelectedItem.IsClosed == false;
        }

        partial void CancelPurchaseOrder_Execute()
        {
            // Write your code here.
            this.PurchaseOrdersOfDrug.DeleteSelected();
        }

        private void updateGoodsInShipping()
        {
            GoodsInShipping = this.PurchaseOrdersOfDrug.Where(orders =>
               orders.IsClosed == false).Cast<PurchaseOrder>().Sum(orders => orders.Quantity);
        }

        partial void DrugStock_InitializeDataWorkspace(List<IDataService> saveChangesTo)
        {
            // Write your code here.
            updateGoodsInShipping();
        }
    }
}